package Operation;

import Book.Book;
import Book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你要删除的书名：");
        String name = scanner.nextLine();

        int currentSize = bookList.getUsedSize();
        //找到图书
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            if (bookList.getBook(i).getName().equals(name)) {
                    index = i;
                    break;
                }
        }
        //index!=-1说明有这本书
        if (index == -1) {
            System.out.println("没有你要删除的书！");
            return;
        }
        for (int j = index; j < currentSize - 1; j++) {
                Book book1 = bookList.getBook(j + 1);
                bookList.setBook(j,book1);
        }
        bookList.setUsedSize(currentSize - 1);

    }
}
